<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>23软件2-23007617-范玉-待办列表</title>
    <link rel="stylesheet" href="http://at.alicdn.com/t/c/font_4537427_scsilhy3vy.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Poppins', sans-serif;
        }

        body {
            height: 100vh;
            width: 100%;
            background-image: url(./背景.webp);
            background-repeat: no-repeat;
            background-size: cover;
        }

        .wrapper {
            margin: 120px auto;
            max-width: 400px;
            width: 100%;
            background: #fff;
            border-radius: 5px;
            padding: 25px;
            box-shadow: 0px 10px 15px rgba(0, 0, 0, 0.1);
        }

        .wrapper header {
            font-size: 30px;
            font-weight: 600;
        }

        .wrapper .inputF {
            display: flex;
            height: 45px;
            width: 100%;
            margin: 20px 0;
        }

        .inputF input {
            width: 86%;
            height: 100%;
            outline: none;
            border: 1px solid #ccc;
            font-size: 17px;
            border-radius: 3px;
            padding-left: 15px;
        }

        .inputF button {
            width: 50px;
            height: 100%;
            border: none;
            outline: none;
            background: cornflowerblue;
            font-size: 22px;
            cursor: pointer;
            margin-left: 10px;
            color: #fff;
            font-size: 22px;
            border-radius: 5px;
            opacity: 0.6;
            pointer-events: none;
        }

        .inputF button.active {
            opacity: 1;
            pointer-events: auto;
        }

        .wrapper .footer {
            display: flex;
            width: 100%;
            align-items: center;
            margin-top: 20px;
            justify-content: space-between;
        }

        .wrapper .todolist {
            max-height: 250px;
            overflow-y: auto;
        }

        .todolist li {
            list-style: none;
            height: 45px;
            line-height: 45px;
            background: rgb(212, 212, 208);
            border-radius: 3px;
            margin-bottom: 8px;
            padding: 0 15px;
            cursor: default;
            overflow: hidden;
            position: relative;
        }

        .todolist li span {
            position: absolute;
            right: -45px;
            color: #fff;
            width: 45px;
            text-align: center;
            background: crimson;
            border-radius: 0 3px 3px 0;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .todolist li:hover span {
            right: 0px;
        }

        .wrapper .footer {
            display: flex;
            width: 100%;
            align-items: center;
            margin-top: 20px;
            justify-content: space-between;
        }

        .footer button {
            border: none;
            outline: none;
            background: cornflowerblue;
            font-size: 22px;
            cursor: pointer;
            margin-left: 3px;
            color: #fff;
            font-size: 16px;
            border-radius: 5px;
            padding: 6px 10px;
            opacity: 0.6;
            pointer-events: none;
        }

        .footer button.active {
            opacity: 1;
            pointer-events: auto;
        }
    </style>
</head>

<body>
    <div class="wrapper">
        <header>待办事件</header>
        <div class="inputF">
            <input type="text" placeholder="Add your new todo">
            <button><i class="iconfont">&#xe600;</i></button>
        </div>
        <ul class="todolist">
        </ul>
        <div class="footer">
            <span>你有 <span class="pendingnumb"></span> 项待办事件</span>
            <button>Clear All</button>
        </div>
    </div>
    <script>
        const inputBox = document.querySelector(".inputF input")
        const addBtn = document.querySelector(".inputF button")
        const todolist = document.querySelector(".todolist")
        const deleteAllBtn = document.querySelector(".footer button")

        inputBox.onkeyup = () => {
            let userData = inputBox.value
            if (userData.trim() != 0) {
                addBtn.classList.add("active")
            } else {
                addBtn.classList.remove("active")
            }
        }
        showTask()

        addBtn.onclick = () => {
            let userDate = inputBox.value
            let getLocalStorage = localStorage.getItem("New Todo")
            if (getLocalStorage == null) {
                listArr = []
            } else {
                listArr = JSON.parse(getLocalStorage)
            }
            listArr.push(userDate)
            localStorage.setItem("New Todo", JSON.stringify(listArr))
            showTask()
            addBtn.classList.remove("active")
        }

        function showTask() {
            let getLocalStorage = localStorage.getItem("New Todo")
            if (getLocalStorage == null) {
                listArr = []
            } else {
                listArr = JSON.parse(getLocalStorage)
            }
            const pendingnumb = document.querySelector(".pendingnumb")
            pendingnumb.textContent = listArr.length
            if (listArr.length > 0) {
                deleteAllBtn.classList.add("active")
            } else {
                deleteAllBtn.classList.remove("active")
            }
            let newLiTag = ''
            listArr.forEach((element, index) => {
                newLiTag += `<li>${element}<span onclick = "deleteTask(${index})"><i class="iconfont">&#xe66f;</i></span></li>`
            })
            todolist.innerHTML = newLiTag
            inputBox.value = ""
        }
        function deleteTask(index) {
            let getLocalStorage = localStorage.getItem("New Todo")
            listArr = JSON.parse(getLocalStorage)
            listArr.splice(index, 1)
            localStorage.setItem("New Todo", JSON.stringify(listArr))
            showTask()
        }
        deleteAllBtn.onclick = () => {
            listArr = []
            localStorage.setItem("New Todo", JSON.stringify(listArr))
            showTask()
        }
    </script>
</body>

</html>